package com.atguigu.flink.chapter11;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.ZoneOffset;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/12/21 10:04
 */
public class Flink09_Time_Event_SQL {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
    
       
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        tEnv.getConfig().setLocalTimeZone(ZoneOffset.ofHours(0));
        
        // bigint        timestamp(3)
        tEnv.executeSql("create table sensor(" +
                            "   id string, " +
                            "   ts bigint, " +
                            "   vc int, " +
                            "   et as TO_TIMESTAMP_LTZ(ts, 3), " +
                            "   watermark for et as et - interval '3' second " +
                            ")with(" +
                            "   'connector'='filesystem', " +
                            "   'path'='input/sensor.txt', " +
                            "   'format'='csv' " +
                            ")");
        
        tEnv.from("sensor").printSchema();
        
        tEnv.sqlQuery("select * from sensor").execute().print();
    }
}
/*
   et2 as TO_TIMESTAMP(from_unixtime(ts/1000)) +
 */